package com.itheima.d6_tcp3;

import java.io.*;
import java.net.Socket;

public class ServerThreadTask implements Runnable {
    private Socket socket;

    public ServerThreadTask(Socket socket) {
        this.socket = socket;
    }

    public ServerThreadTask() {
    }

    @Override
    public void run() {

        try (Socket serverSocket = this.socket) {
            // 3、从socket通信管道中得到一个字节输入流。
            InputStream is = serverSocket.getInputStream();
            // 4、把原始的字节输入流包装成高效的输入流
            BufferedReader netInt = new BufferedReader(new InputStreamReader(is));
            //   DataInputStream dis=new DataInputStream(is);

            while (true) {
                try {
                    // 5、使用数据输入流读取客户端发送过来的消息
                    String readLine = netInt.readLine();
                    System.out.println(readLine);
                } catch (IOException e) {
                    System.out.println("离线了");
                    netInt.close();
                    is.close();
                    //  dis.close();
                    serverSocket.close();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
